package com.canbot.u05.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.AppInfo;

import java.util.List;

/**
 * 首页适配器
 */

public class AppRecyclerViewAdapter extends RecyclerView.Adapter<AppRecyclerViewAdapter.MyViewHolder> {

        List<AppInfo> mbrowList;

        LayoutInflater inflater;

        Context mContext;

        private OnItemClickLisener lisener;

        private String[] moduleName;

        private TypedArray moduleDrawables;

        public AppRecyclerViewAdapter(Context mContext, List<AppInfo> mbrowList) {
                this.mbrowList = mbrowList;
                this.mContext = mContext;
                inflater = LayoutInflater.from(mContext);
                moduleName = mContext.getResources().getStringArray(R.array.order_edit_module_names);
                moduleDrawables = mContext.getResources().obtainTypedArray(R.array.order_edit_module_drawables);
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

                public TextView iconName;

                private ImageView iconImageview;

                public MyViewHolder(View view) {
                        super(view);
                        iconName = (TextView) view.findViewById(R.id.text_industry);
                        iconImageview = (ImageView) view.findViewById(R.id.btn_industry);
                }
        }

        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.industry_item, parent,false);
                return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
                AppInfo appListItemBean = mbrowList.get(position);

                for (int i = 0; i < moduleName.length; i++) {

                        if(appListItemBean.getAppName().equals(moduleName[i])){
                            String nickname = appListItemBean.getNickname();
                            String appName = appListItemBean.getAppName();
                            String showName = TextUtils.isEmpty(nickname) ? appName : nickname;
                            holder.iconName.setText(showName);
                            holder.iconImageview.setImageDrawable(mContext.getDrawable(moduleDrawables.getResourceId(i,0)));
                        }
                }

                holder.iconImageview.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                lisener.getPosition(appListItemBean);
                        }
                });
        }

        @Override
        public long getItemId(int i) {
                return i;
        }

        @Override
        public int getItemCount() {
                if (mbrowList == null) {
                        return 0;
                }
                return mbrowList.size();

        }

        public void setOnItemClickLisener(OnItemClickLisener lisener){
                this.lisener = lisener;
        }

        public interface OnItemClickLisener{
                void getPosition(AppInfo info);
        }

}
